/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import entities.Report;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Thanh
 */
public class GetReportDAO {
public Report getReport(String id) {
        Report result = new Report();
        try {
            String un = "sa";
            String pw = "1234";
            String url = "jdbc:sqlserver://localhost:1433;databaseName=BloggerDB;";
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = java.sql.DriverManager.getConnection(url, un, pw);

            String sql = "select r.ReportID, u.Username, u.FullName, t.Title, r.Content, r.IsCheck, r.CreatedTime from Reportes r left join Users u on r.UserID = u.UserID left join Topics t on r.TopicID = t.TopicID where ReportID = ?";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, id);
            ResultSet rs = stm.executeQuery();

            while (rs.next()) {
                result.setReportID(rs.getInt("ReportID"));
                result.setUsername(rs.getString("Username"));
                result.setFullName(rs.getString("FullName"));
                result.setTitle(rs.getString("Title"));
                result.setContent(rs.getString("Content"));
                result.setIsCheck(rs.getBoolean("IsCheck"));
                result.setCreatedTime(rs.getDate("CreatedTime").toString() + " " + rs.getTime("CreatedTime").toString());
            }
            rs.close();
            stm.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
